package repository

import (
	"context"
	"gitee.com/jinmingzhi/cdce-service/internal/model"
	"gitee.com/jinmingzhi/kit/rdb"
)

const (
	userAddField       = "user,passwd,gender,registration_number,student_id,name,image_url,create_time"
	userField          = "id,user,passwd,gender,registration_number,student_id,name,image_url,create_time,delete_time"
	userDetailField    = "id, uid, nation, student_status, occupation_status, certificate_type, school_name, cultivation_level, id_number"
	userDetailAddField = "uid, nation, student_status, occupation_status, certificate_type, school_name, cultivation_level, id_number"
	userAddSql         = "insert into `" + USER + "`(" + userAddField + ") values(?,?,?,?,?,?,?,?)"
	userDetailAddSql   = "insert into `" + USERDETAIL + "`(" + userDetailAddField + ") values(?,?,?,?,?,?,?,?)"
	userSetPasswdSql   = "update `" + USER + "` set passwd = ? where id = ?"
	findUserSql        = "select " + userField + " from `" + USER + "` where user=?"
	findUserByUidSql   = "select " + userField + " from `" + USER + "` where id=?"
	findUserDetail     = "select " + userDetailField + " from `" + USERDETAIL + "` where uid=?"
)

type User interface {
	AddUser(ctx context.Context, data *model.User) error
	AddUserDetail(ctx context.Context, data *model.UserDetail) error
	FindUser(ctx context.Context, user string) (*model.User, error)
	FindUserByUid(ctx context.Context, uid uint32) (*model.User, error)
	FindUserDetail(ctx context.Context, uid uint32) (*model.UserDetail, error)
	SetPasswd(ctx context.Context, uid uint32, passwd string) error
}

type UserImpl struct {
	repo rdb.Repository
}

func NewUser(repo rdb.Repository) *UserImpl {
	return &UserImpl{repo: repo}
}

func (u *UserImpl) AddUser(ctx context.Context, data *model.User) error {
	_, err := u.repo.AddOne(ctx, userAddSql, data.User, data.Passwd, data.Gender, data.RegistratonNumber, data.StudentID, data.Name, data.ImageUrl, data.CreateTime)
	if err != nil {
		return err
	}
	return nil
}

func (u *UserImpl) AddUserDetail(ctx context.Context, data *model.UserDetail) error {
	_, err := u.repo.AddOne(ctx, userDetailAddSql, data.Uid, data.Nation, data.StudentStatus, data.OccupationStatus, data.CertificateType, data.SchoolName, data.CultivationLevel, data.IDNumber)
	if err != nil {
		return err
	}
	return nil
}

func (u *UserImpl) FindUser(ctx context.Context, user string) (*model.User, error) {
	data := &model.User{}
	err := u.repo.FindOne(ctx, data, findUserSql, user)
	if err != nil {
		return nil, err
	}
	return data, nil
}

func (u *UserImpl) FindUserByUid(ctx context.Context, uid uint32) (*model.User, error) {
	data := &model.User{}
	err := u.repo.FindOne(ctx, data, findUserByUidSql, uid)
	if err != nil {
		return nil, err
	}
	return data, nil
}

func (u *UserImpl) FindUserDetail(ctx context.Context, uid uint32) (*model.UserDetail, error) {
	data := &model.UserDetail{}
	err := u.repo.FindOne(ctx, data, findUserDetail, uid)
	if err != nil {
		return nil, err
	}
	return data, nil
}

func (u *UserImpl) SetPasswd(ctx context.Context, uid uint32, passwd string) error {
	return u.repo.SaveOne(ctx, userSetPasswdSql, passwd, uid)
}
